using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// 装备类型枚举
public enum EquipmentType
{
    Weapon, // 武器
    Armor, // 盔甲
    Amulet, // 护身符
    Flask // 药水瓶
}

// 创建资源菜单，默认名称为 "New Item Data"，菜单名称为 "Data/Equipment"，排序为 1
[CreateAssetMenu(fileName = "New Item Data", menuName = "Data/Equipment", order = 1)]
public class ItemData_Equipment : ItemData
{

    public float itemCooldown;

    [Header("装备类型")]
    public EquipmentType equipmentType;

    [Header("装备效果")]
    public ItemEffect[] itemEffects;

    [Header("主要属性")]
    public int strength; // 力量
    public int agility; // 敏捷
    public int intelligence; // 智力
    public int vitality; // 生命力

    [Header("攻击属性")]
    public int damage; // 伤害
    public int critChance; // 暴击率
    public int critPower; // 暴击伤害

    [Header("防御属性")]
    public int health; // 生命值
    public int armor; // 护甲
    public int evasion; // 闪避
    public int magicResistance; // 魔法抗性

    [Header("魔法属性")]
    public int fireDamage; // 火焰伤害
    public int iceDamage; // 冰霜伤害
    public int lightingDamage; // 闪电伤害

    [Header("材料清单")]
    public List<InventoryItem> craftingMaterials;

    private int descriptionLength;

    // 遍历装备效果并使用
    public void ExecuteItemEffect(Transform _enemyPosition)
    {
        if (itemEffects.Length <= 0) return;
        foreach (var item in itemEffects)
        {
            item.ExecuteEffect(_enemyPosition);
        }
    }

    // 添加属性修正
    public void AddModifiers()
    {
        PlayerStats playerStats = PlayerManager.instance.player.GetComponent<PlayerStats>();

        playerStats.strength.AddModifier(strength);
        playerStats.agility.AddModifier(agility);
        playerStats.intelligence.AddModifier(intelligence);
        playerStats.vitality.AddModifier(vitality);

        playerStats.damage.AddModifier(damage);
        playerStats.critChance.AddModifier(critChance);
        playerStats.critPower.AddModifier(critPower);

        playerStats.maxHealth.AddModifier(health);
        playerStats.armor.AddModifier(armor);
        playerStats.evasion.AddModifier(evasion);
        playerStats.magicResistance.AddModifier(magicResistance);

        playerStats.fireDamage.AddModifier(fireDamage);
        playerStats.iceDamage.AddModifier(iceDamage);
        playerStats.lightingDamage.AddModifier(lightingDamage);
    }

    // 移除属性修正
    public void RemoveModifiers()
    {
        PlayerStats playerStats = PlayerManager.instance.player.GetComponent<PlayerStats>();

        playerStats.strength.RemoveModifier(strength);
        playerStats.agility.RemoveModifier(agility);
        playerStats.intelligence.RemoveModifier(intelligence);
        playerStats.vitality.RemoveModifier(vitality);

        playerStats.damage.RemoveModifier(damage);
        playerStats.critChance.RemoveModifier(critChance);
        playerStats.critPower.RemoveModifier(critPower);

        playerStats.maxHealth.RemoveModifier(health);
        playerStats.armor.RemoveModifier(armor);
        playerStats.evasion.RemoveModifier(evasion);
        playerStats.magicResistance.RemoveModifier(magicResistance);

        playerStats.fireDamage.RemoveModifier(fireDamage);
        playerStats.iceDamage.RemoveModifier(iceDamage);
        playerStats.lightingDamage.RemoveModifier(lightingDamage);
    }

    //获取属性描述
    public override string GetDescription()
    {
        sb.Length = 0; // 重置字符串构建器
        descriptionLength = 0; // 重置描述长度
        AddItemDescription(strength, "Strength"); // 添加力量描述
        AddItemDescription(agility, "Agility"); // 添加敏捷描述
        AddItemDescription(intelligence, "Intelligence"); // 添加智力描述
        AddItemDescription(vitality, "Vitality"); // 添加活力描述

        AddItemDescription(damage, "Damage"); // 添加伤害描述
        AddItemDescription(critChance, "Crit.Chance"); // 添加暴击几率描述
        AddItemDescription(critPower, "Crit.Power"); // 添加暴击力量描述

        AddItemDescription(health, "Health"); // 添加生命值描述
        AddItemDescription(evasion, "Evasion"); // 添加闪避描述
        AddItemDescription(armor, "Armor"); // 添加护甲描述
        AddItemDescription(magicResistance, "Magic Resist."); // 添加魔法抗性描述

        AddItemDescription(fireDamage, "Fire damage"); // 添加火焰伤害描述
        AddItemDescription(iceDamage, "Ice damage"); // 添加冰霜伤害描述
        AddItemDescription(lightingDamage, "Lighting dmg."); // 添加闪电伤害描述

        for (int i = 0; i < itemEffects.Length; i++)
        {
            if (itemEffects[i].EffectDescription?.Length > 0)
            {
                sb.AppendLine(); // 添加空行
                sb.Append(itemEffects[i].EffectDescription); // 添加空字符串
                descriptionLength++;
            }
        }

        // 限制描述最少5条，防止装备信息提示框过小
        if (descriptionLength < 5)
        {
            for (int i = 0; i < 5 - descriptionLength; i++)
            {
                sb.AppendLine(); // 添加空行
                sb.Append(""); // 添加空字符串
            }
        }


        return sb.ToString(); // 返回描述字符串
    }

    // 增加属性描述
    private void AddItemDescription(int _value, string _name)
    {
        if (_value != 0)
        {
            if (sb.Length > 0)
                sb.AppendLine(); // 添加换行符
            if (_value > 0)
                sb.Append("+" + _value + ": " + _name); // 添加属性描述
            descriptionLength++; // 增加描述长度
        }
    }
}